//
//  newsImageViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/22.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "newsImageViewController.h"
#import "common.h"
#import "DataService.h"
#import "imageCell.h"
#import "photoViewController.h"
#import "BaseNavigationController.h"

@interface newsImageViewController ()
@end

@implementation newsImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图片新闻";
    [self _collectionView];
    [self _loadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - _createSubviews
- (void)_collectionView{
 //   self.tabBarController.tabBar.hidden = YES;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((Kwidth-50)/4, (Kwidth-50)/4);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"cellId"];
    
    [self.view addSubview:_collectionView];
}

#pragma mark - _loadData

- (void)_loadData{
    _modalArray = [[NSMutableArray alloc] init];
    
    NSArray *array = [DataService initWitnJsionFileName:imageListFile];
    for (NSDictionary *dic in array) {
        imageModal *modal = [[imageModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_modalArray addObject:modal];
    }
    
    
}

#pragma mark - degelate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _modalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.modal = _modalArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    photoViewController *vc = [[photoViewController alloc] init];
    vc.view.backgroundColor = [UIColor greenColor];
    
    vc.currentIndex = indexPath.row;
    BaseNavigationController *nav =[[ BaseNavigationController alloc] initWithRootViewController:vc];
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc] init];
    for (imageModal *modal in _modalArray) {
        [imageUrlArray addObject:modal.image];
    }
    vc.imageUrlArray = imageUrlArray;
    
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
